package com.mos.simple.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: <br/>
 * date: 2024/12/26 17:34<br/>
 *
 * @author 小尘哥<br />
 */
public class WordGen {

    public static void main(String[] args) {

        // 渲染的数据
        Map<String, Object> map = new HashMap<>();

        /*文本*/
        txtSimple(map);

        //图表
        chartSimple(map);

        //表格
        tableSimple(map);

        //图片
        imgSimple(map);


        try {
            ClassLoader classLoader = WordGen.class.getClassLoader();
            URL resourceUrl = classLoader.getResource("demo/wordGen.docx");
            XWPFTemplate.compile(resourceUrl.getFile()).render(map).writeToFile("D:\\2.docx");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void imgSimple(Map<String, Object> map) {
        try {
            ClassLoader classLoader = WordGen.class.getClassLoader();
            URL resourceUrl = classLoader.getResource("static//images//star-bg.jpg");
            map.put("imgSimple", Pictures.ofStream(new FileInputStream(resourceUrl.getFile()), PictureType.JPEG)
                    .size(500, 500).create());

            URL myQrCodeUrl = classLoader.getResource("static//images//qrcode.jpg");
            map.put("myQr", Pictures.ofStream(new FileInputStream(myQrCodeUrl.getFile()), PictureType.JPEG)
                    .size(300, 300).create());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static void tableSimple(Map<String, Object> map) {
        RowRenderData row0 = Rows.of("姓名", "性别", "年龄", "班级").textColor("FFFFFF")
                .bgColor("4472C4").center().create();
        TableRenderData renderData = Tables.create(row0);
        for (int i = 0; i < 50; i++) {
            String gen = i % 3 == 1 ? "男" : "女";
            RowRenderData row = Rows.create("樱木花道", gen, "17", "三年二班");
            renderData.addRow(row);
        }
        map.put("tableSimple", renderData);
    }

    private static void chartSimple(Map<String, Object> map) {
        map.put("lineChart", Charts.ofLine("变化趋势", new String[]{"2021", "2022", "2023", "2024", "2025", "2026"})
                .addSeries("数量", new Number[]{1, 2, 3, 4, 5, 6}).create());
        //单系列图表指的是饼图（3D饼图）、圆环图等。
        map.put("chart1", Charts
                .ofSingleSeries("综合测评结果统计", new String[]{"正常", "异常", "其他"})
                .series("badAndgood", new Integer[]{19, 17, 15})
                .create());
        map.put("barChart", Charts.ofBar3D("GDP", new String[]{"2021", "2022", "2023", "2024"})
                .addSeries("数量", new Number[]{100, 300, 320, 380})
                .create());
    }

    private static void txtSimple(Map<String, Object> map) {
        map.put("name", "小尘哥");
        map.put("author", new TextRenderData("cccccc", "小尘哥"));
        map.put("poilink", new HyperlinkTextRenderData("poi-tl官方网站", "https://deepoove.com/poi-tl/"));
        map.put("mygiteelink", new HyperlinkTextRenderData("我的gitee", "https://gitee.com/chemors/mos-simple.git"));
    }
}
